package com.hbsx.why.ui.find;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hbsx.why.NormalMainActivity;
import com.hbsx.why.R;
import com.hbsx.why.TabMainActivity;
import com.hbsx.why.adapter.FindListAdapter;
import com.hbsx.why.base.BaseFragment;
import com.hbsx.why.utils.refresh.PullListView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.hbsx.why.views.BackButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FindFragment extends BaseFragment {
    public static TabMainActivity myTabActivity;
    private static String mTitle;
    private static String BUNDLE_flag = "BUNDLE_flag";
    @BindView(R.id.btn_back)
    BackButton btnBack;
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.btn_top_right)
    ImageView btnTopRight;
    @BindView(R.id.text_top_right)
    TextView textTopRight;
    @BindView(R.id.find_PullListView)
    PullListView findPullListView;
    @BindView(R.id.find_PullToRefreshLayout)
    PullToRefreshLayout findPullToRefreshLayout;
    private Context context;
    private FindListAdapter findlistAdapter;
    private int flag;
    private List<String> stringfindList;
    private int page = 0;
    public static NormalMainActivity myNormalMainActivity;

    @Override
    protected int getLayout() {
        return R.layout.ui_find_fragment;
    }

    @Override
    protected void onInit() {
        tvTopTitle.setText(mTitle);
        Bundle arguments = getArguments();
        if (arguments != null) {
            flag = arguments.getInt(BUNDLE_flag);
        }
        btnBack.setVisibility(View.GONE);
        btnTopRight.setVisibility(View.VISIBLE);
        context = getActivity();
        Glide.with(context).load(R.mipmap.ic_search_white).into(btnTopRight);
        //LogUtils.logi("---FindFragment---flag="+flag);
        findlistAdapter = new FindListAdapter(context, stringfindList);
        findPullListView.setAdapter(findlistAdapter);
        page = 1;
        getData(page);
        findPullListView.setPullDownEnable(true);
        findPullListView.setPullUpEnable(false);
        findPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        findPullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = page + 1;
                        getData(page);
                        findPullToRefreshLayout.loadMoreFinish(true);
                    }
                }, 1000);
            }
        });
    }

    private void getData(int page) {
        if (page == 1) {
            stringfindList = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                stringfindList.add(flag + "数据刷新--" + i);
            }
            findlistAdapter.updateListView(stringfindList);
        } else {
            stringfindList = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                stringfindList.add(flag + "加载数据--" + page + i);
            }
            findlistAdapter.loadMoreListView(stringfindList);
        }
        findlistAdapter.notifyDataSetChanged();
        //mPullToRefreshLayout.autoLoadMove(true);
        // mPullToRefreshLayout.getRefreshFooterView().setVisibility(View.GONE);
    }

    @Override
    public void fetchData() {
        page = 1;
        getData(page);
    }

    public static FindFragment newInstance() {
        FindFragment fragment = new FindFragment();
        return fragment;
    }

    public static FindFragment newInstance(String title, int flag) {
        FindFragment fragment = new FindFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_flag, flag);
        mTitle = title;
        fragment.setArguments(bundle);
        return fragment;
    }

    //    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//    }
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        // TODO: inflate a fragment view
//        View rootView = super.onCreateView(inflater, container, savedInstanceState);
//        ButterKnife.bind(this, rootView);
//        return rootView;
//    }

    @OnClick({R.id.btn_top_right})
    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.chang_tab_one:
//
//                break;
//        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
